package com.cn.yang.servlet.contactUs;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.model.contactUs.ContactUsModel;
import com.cn.yang.model.message.MessageModel;
import com.cn.yang.service.ContactUsService;
import com.cn.yang.service.MessageService;
import com.cn.yang.service.impl.ContactUsServiceImpl;
import com.cn.yang.service.impl.MessageServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/contactUs/updateContactUs")
public class UpdateContactUsServlet extends HttpServlet {
    //查询数据，返回==============
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        Integer id = Integer.valueOf(req.getParameter("id"));
        ContactUsService service = new ContactUsServiceImpl();
        ContactUsModel model = service.selectById(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("contactUsModel",model);
        resp.getWriter().println(jsonObject);
    }
    // 更新数据库===========================================================
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String location = req.getParameter("location");
        String contactphone = req.getParameter("contactphone");
        String orderphone = req.getParameter("orderphone");
        String email = req.getParameter("email");
        String net = req.getParameter("net");
        String line = req.getParameter("line");
        String wechat = req.getParameter("wechat");
        String twitter = req.getParameter("twitter");
        ContactUsModel model = new ContactUsModel();
        model.setLocation(location);
        model.setContactphone(contactphone);
        model.setOrderphone(orderphone);
        model.setEmail(email);
        model.setNet(net);
        model.setLine(line);
        model.setWechat(wechat);
        model.setTwitter(twitter);

        ContactUsService service = new ContactUsServiceImpl();
        int result = service.updateContactUs(model);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);

    }
}
